<?php
/**
 * Notes: 任务模块
 * Created by PhpStorm.
 * Author: TS
 * Date: 2019/11/4
 * Time: 11:22
 */

namespace app\index\controller;

use app\model\Task as TaskModel;
use app\model\UserReal;
use app\model\Users as UsersModel;
use app\validate\TaskValidate;
use app\model\TaskStage as TaskStageModel;
class Task
{
    /**
     *
     * @return false|string
     */
    public function getMyTask(){
        if(request()->isPost()){
            $user_id = getNowUserId();
            $TaskModel = new TaskModel();
            $my_task = $TaskModel->getMyTask($user_id);
            return res(1,'',$my_task);
        }
        return res(0,MSG_NOT_POST);

    }

    /**
     * @return false|string
     */
    public function getMyCreateTask(){
        if(request()->isPost()){
            $user_id = getNowUserId();
            $TaskModel = new TaskModel();
            $task_data = $TaskModel->getMyRelease($user_id);
            return res(1,'',$task_data);
        }
        return res(0,MSG_NOT_KEY);
    }
    /**
     *
     * @return false|string
     */
    public function releaseTask(){
        if(request()->isPost()){
            $user_id = getNowUserId();
            if(!checkNull($user_id)) return res(0,'账户未登录');
            $data = request()->only('task_title,task_content,task_start,task_end,task_target');
            $TaskValidate = new TaskValidate();
            if(!$TaskValidate->check($data)) return res(0,$TaskValidate->getError());
            $UserModel = new UsersModel();

            $data['release_time'] = time();
            $data['task_status'] = 1;
            $data['task_progress'] = 0;
            $data['user_id'] = $user_id;
            $data['task_start'] = strtotime($data['task_start']);
            $data['task_end'] = strtotime($data['task_end']);
            $TaskModel = new TaskModel();
            $TaskModel->startTrans();
            $targets = explode("-",$data['task_target']);
            $TaskStageModel = new TaskStageModel();
            $TaskStageModel->startTrans();
            $stage_data = [
                'stage_content' => '指派任务',
                'task_progress' => 0,
                'stage_time' => time()
            ];
            foreach ($targets as $target){

                $data['task_target'] = $target;
                $check_user = $UserModel->getUserById($data['task_target']);
                if(!$check_user) return res(0,'指派人不存在');
                $release_res = $TaskModel->add($data);

                if($release_res) {

                    $stage_data['task_id'] = $release_res['task_id'];
                    $add_stage_res = $TaskStageModel->add($stage_data);
                    if(!$add_stage_res){
                        $TaskModel->rollback();
                        $TaskStageModel->rollback();
                        return res(0,'指派失败');
                    }

                }else{
                    $TaskModel->rollback();
                    $TaskStageModel->rollback();
                    return res(0,'指派失败');
                }
            }
            $TaskStageModel->commit();
            $TaskModel->commit();
            return res(1,'指派成功',$targets);
        }
        return res(0,MSG_NOT_POST);
    }

    /**
     *
     * @return false|string
     */
    public function changeTaskStatus(){
        if(request()->isPost()){
            $task_id = input('task_id');
            $task_status = input('task_status');
            if(!checkNull($task_id) || !checkNull($task_status)) return res(0,MSG_NOT_KEY);
            $user_id = getNowUserId();
            $TaskModel = new TaskModel();
            $task = $TaskModel->getByIdAndUser($task_id,$user_id);
            if(!$task) return res(0,'无该任务');
            $update_data = [
                'task_status' => $task_status
            ];
            $res_change = $TaskModel->edit($task_id,$update_data);
            if($res_change){
                return res(1,'更新任务进度成功');
            }
            return res(0,'更新失败');
        }
        return res(0,MSG_NOT_POST);
    }
    public function getTaskById(){
        $task_id = input('task_id');
        if(!checkNull($task_id)) return res(0,MSG_NOT_KEY);
        $TaskModel = new TaskModel();
        $UserRealModel = new UserReal();
        $task_res = $TaskModel->getTaskData($task_id);
        $target = $UserRealModel->getByUserId($task_res['target_name']);
        $task_res['target_name'] = $target['real_name'];
        return res(1,'',$task_res);
    }
    public function getTaskTarget(){
        if(request()->isPost()){
            $mod = input('post.mode');
            $UserModel = new UsersModel();
            $target = [];
            if(isset($mod) && $mod == "department"){
                $user_id = getNowUserId();
                $department_id = $UserModel->getUserById($user_id)["department_id"];
                $target = $UserModel->getMyDepartmentTaskTarget($department_id);
            }else{
                $target = $UserModel->getTaskTarget();
            }

            return res(1,'',$target);
        }
        return res(0,MSG_NOT_POST);
    }

    public function getStage(){
        $task_id = input('task_id');
        $TaskStageModel = new TaskStageModel();
        $task_stage = $TaskStageModel->getByTask($task_id);
        return res(1,'',$task_stage);
    }
    public function addStage(){
        $data = request()->only('stage_content,task_id,task_progress');
        if(!isset($data['stage_content']) || !isset($data['task_id']) || !isset($data['task_progress'])) return res(0,MSG_PARAM);
        $TaskModel = new TaskModel();
        $task_check = $TaskModel->getById($data['task_id']);
        if(!$task_check) return res(0,"无该任务");
        if($task_check["task_status"] != 1) return res(0,"该任务不在进行中");
        $user_id = getNowUserId();
        if($task_check["task_target"] != $user_id) return res(0,"您不是该任务受派人");
        $data['stage_time'] = time();
        $TaskStageModel = new TaskStageModel();
        $TaskStageModel->startTrans();
        $add_stage_res = $TaskStageModel->add($data);
        if(!$add_stage_res) return res(0,'添加失败');

        $task_update = [
            'task_progress' => $data['task_progress']
        ];
        if($task_update["task_progress"] == "100") $task_update["task_status"] = 2;
        $update_res = $TaskModel->edit($data['task_id'],$task_update);
        if(!$update_res){
            $TaskStageModel->rollback();
            return res(0,'添加失败');
        }
        $TaskStageModel->commit();
        return res(1,'添加成功');
    }


}